import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
} from '@fast-crud/fast-crud';

import type { FormRulesExt } from '#/api';
import type { DefParameterModel } from '#/api/devOperation/system/model/defParameterModel';

import { ref } from 'vue';

import { useAccess } from '@vben/access';
import { $t } from '@vben/locales';

import { RuleType } from '#/api';
import { DefParameterApi } from '#/api/devOperation/system/defParameter';
import { PermCode } from '#/enums/perm';
import {
  createdTimeColumn,
  deleteButton,
  indexColumn,
  STATE_CONSTANT_DICT,
} from '#/plugins/fast-crud/common';

const { hasPermission } = useAccess();
const permCode = PermCode.devOperation.system.defParameter;

export function createCrudOptions(
  props: CreateCrudOptionsProps<DefParameterModel.DefParameterResultVO, any>,
): CreateCrudOptionsRet<DefParameterModel.DefParameterResultVO> {
  const selectedRowKeys = ref<string[]>([]);

  return {
    crudOptions: {
      request: {
        pageRequest: DefParameterApi.pageRequest,
        addRequest: DefParameterApi.addRequest,
        editRequest: DefParameterApi.editRequest,
        delRequest: DefParameterApi.delRequest,
        infoRequest: DefParameterApi.infoRequest,
      },
      actionbar: {
        buttons: {
          add: { show: hasPermission(permCode.add) },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            removeFn: DefParameterApi.remove,
            role: permCode.delete,
          }),
        },
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        width: '200px',
        buttons: {
          edit: { show: hasPermission(permCode.edit) },
          view: { show: true },
          remove: { show: hasPermission(permCode.delete) },
          copy: { show: hasPermission(permCode.add) },
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        name: {
          title: $t('devOperation.system.defParameter.name'),
          type: 'text',
          search: { show: true },
        },
        key: {
          title: $t('devOperation.system.defParameter.key'),
          type: 'text',
          search: { show: true },
        },
        value: {
          title: $t('devOperation.system.defParameter.value'),
          type: 'text',
          search: { show: true },
        },
        state: {
          title: $t('devOperation.system.defParameter.state'),
          type: 'dict-radio',
          dict: STATE_CONSTANT_DICT,
          search: { show: true, component: { mode: 'multiple' } },
          addForm: { value: true },
        },
        remarks: {
          title: $t('devOperation.system.defParameter.remarks'),
          type: 'textarea',
        },
        ...createdTimeColumn({}),
      },
    },
  };
}

export const frontRules = {
  addForm: {
    rules: (): FormRulesExt => {
      return {
        key: {
          type: RuleType.and,
          rules: [
            {
              trigger: 'blur',
              type: 'string',
              message: '参数重复',
              asyncValidator: async (_: any) => {
                /* if (value && (await DefParameterApi.check(value))) {
                  throw new Error('参数重复');
                }*/
              },
            },
          ],
        },
      };
    },
  },
  editForm: {
    rules: (): FormRulesExt => {
      return {
        key: {
          type: RuleType.and,
        },
      };
    },
  },
};
